package org.itboys.core.service.mongo.front;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dto.gift.SundayGiftDto;
import org.itboys.core.entity.mongo.SundayGift;
import org.itboys.core.entity.mongo.SundayMember;
import org.itboys.core.entity.mongo.SundayScene;
import org.itboys.core.service.mongo.web.SundayMemberService;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 作者：zzq
 * 时间：2018/10/26
 * 描述：礼物
 */
@Service
public class SundayFrontGiftService extends BaseMongoService<SundayGift> {
    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;
    @Autowired
    private ResourceHolder resourceHolder;
    @Autowired
    private SundayMemberService memberService;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayGift> getEntityClass() {
        return SundayGift.class;
    }

    public List<SundayGiftDto> select(Long memberId, Integer status, Integer searchType) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        //查询送出的礼物
        if (searchType == 1) {
            param.put("sendId", memberId);
            //查询收到的礼物
        } else if (searchType == 2) {
            param.put("receiveId", memberId);
            param.put("status", status);
        }
        return packageMultiDto(super.list(param));
    }

    public SundayGiftDto findOne(Long id) {
        return packageSingleDto(super.getById(id));
    }

    /**
     * 组装单个
     *
     * @param gift
     * @return
     */
    private SundayGiftDto packageSingleDto(SundayGift gift) {
        SundayGiftDto dto = new SundayGiftDto();
        BeanUtils.copyProperties(gift, dto);
        SundayMember sendMember = memberService.getById(gift.getSceneId());
        SundayMember receiveMember = memberService.getById(gift.getReceiveId());
        dto.setSendName(sendMember.getNickName());
        dto.setReceiveName(receiveMember.getNickName());
        return dto;
    }

    /**
     * 组装多个
     *
     * @param gifts
     * @return
     */
    public List<SundayGiftDto> packageMultiDto(List<SundayGift> gifts) {
        List<SundayGiftDto> dtos = new ArrayList<>();
        if(ListUtil.isNotNull(gifts)){
            List<Long> sendIds=gifts.stream().map(e -> e.getSendId()).collect(Collectors.toList());
            List<Long> receiveIds=gifts.stream().map(e -> e.getReceiveId()).collect(Collectors.toList());
            Map<String,Object> param=new HashMap<String,Object>();
            param.put("isDeleted",SundayCommonConstants.NO);
            param.put("id in",sendIds);
            List<SundayMember> sendMembers = memberService.list(param);
            param.clear();
            param.put("isDeleted",SundayCommonConstants.NO);
            param.put("id in",receiveIds);
            List<SundayMember> receiveMembers = memberService.list(param);
            for (SundayGift gift :gifts) {
                SundayGiftDto dto = new SundayGiftDto();
                BeanUtils.copyProperties(gift, dto);
                for (SundayMember sendMember :sendMembers) {
                    if(gift.getSendId().longValue()==sendMember.getId()){
                        dto.setSendName(sendMember.getNickName());
                        break;
                    }
                }
                for (SundayMember receiveMember :receiveMembers) {
                    if(gift.getReceiveId().longValue()==receiveMember.getId()){
                        dto.setReceiveName(receiveMember.getNickName());
                        break;
                    }
                }
                dtos.add(dto);
            }
        }
        return dtos;
    }


}
